package org.onion.pattern.notify.dingtalk;


import jakarta.annotation.Nonnull;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.onion.pattern.notify.dingtalk.impl.DingTalkRobotDumbNotifier;
import org.onion.pattern.notify.dingtalk.impl.DingTalkRobotNotifierImpl;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.annotation.Nonnull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Objects;
import java.util.Optional;

@Component
@Setter
@Slf4j
//@ConditionalOnProperty(name = "popjoy.notify.dingtalk.robot.enable", matchIfMissing = true)
public class DingTalkRobot {

    @Autowired(required = false)
    private DingTalkRobotConfigProvider propProvider;
    @Autowired(required = false)
    private DingTalkRobotProperties properties;
    private DingTalkRobotNotifyExecutor executor;

    public DingTalkRobot() {
        this.executor = DingTalkRobotNotifyExecutor.EXECUTOR;
    }

    @PostConstruct
    public void init() {
        if (!properties.getEnable()) {
            return;
        }
        this.executor = new DingTalkRobotNotifyExecutor(ObjectUtils.defaultIfNull(properties.getExecutorThreadSize(), 1),
                ObjectUtils.defaultIfNull(properties.getExecutorQueueSize(), 1000));
        this.executor.setNotifyUrl(properties.getNotifyUrl());
    }

    @PreDestroy
    public void close() {
        this.executor.close();
    }

    public Optional<DingTalkRobotNotifier> findRobotByConfigId(@Nonnull String configId, @Nonnull String theme) {
        Assert.hasText(configId,"");
        Assert.hasText(theme,"");

        if (properties != null && !ObjectUtils.defaultIfNull(properties.getEnable(), true)) {
            return Optional.of(DingTalkRobotDumbNotifier.NOTIFIER);
        }

        DingTalkRobotProperties.NotifyThemeConf conf = null;
        if (propProvider != null && StringUtils.isNumeric(configId)) {
            Long brandId;
            try {
                brandId = Long.valueOf(configId);
                Assert.isTrue(Objects.requireNonNull(brandId) > 0, "");
                conf = propProvider.findConfig(brandId, theme);
            } catch (NumberFormatException e) {
                //不需要处理
            }
        }

        if (conf == null && properties != null) {
            conf = properties.findConfig(configId, theme).orElse(null);
        }

        if (conf == null) {
            return Optional.empty();
        }

        return Optional.of(new DingTalkRobotNotifierImpl(conf.getAccessToken(), this.executor)
                .setSignKey(conf.getSignKey()));
    }

    public DingTalkRobotNotifier newRobot() {
        if (!Objects.requireNonNull(properties, "properties 为空").getEnable()) {
            return DingTalkRobotDumbNotifier.NOTIFIER;
        }

        return new DingTalkRobotNotifierImpl(properties.getAccessToken(), this.executor)
                .setSignKey(properties.getSignKey());
    }

    public DingTalkRobotNotifier newRobot(@Nonnull String accessToken) {
        Assert.hasText(accessToken,"");
        if (properties != null && !ObjectUtils.defaultIfNull(properties.getEnable(), true)) {
            return DingTalkRobotDumbNotifier.NOTIFIER;
        }

        return new DingTalkRobotNotifierImpl(accessToken, this.executor);
    }

    public DingTalkRobotNotifier newRobot(@Nonnull String accessToken,@Nonnull String signKey) {
        Assert.hasText(accessToken,"");
        Assert.hasText(signKey,"");
        if (properties != null && !ObjectUtils.defaultIfNull(properties.getEnable(), true)) {
            return DingTalkRobotDumbNotifier.NOTIFIER;
        }

        return new DingTalkRobotNotifierImpl(accessToken, this.executor)
                .setSignKey(signKey);
    }

    public DingTalkRobotNotifier newRobotFromConfigId(@Nullable String confId, @Nullable String theme) {
        if (!Objects.requireNonNull(properties, "properties 为空").getEnable()) {
            return DingTalkRobotDumbNotifier.NOTIFIER;
        }

        DingTalkRobotProperties.NotifyThemeConf conf = null;

        if (propProvider != null && StringUtils.isNotBlank(confId) && StringUtils.isNumeric(confId)) {
            Long brandId;
            try {
                brandId = Long.valueOf(confId);
                Assert.isTrue(Objects.requireNonNull(brandId) > 0, "");
                conf = propProvider.findConfig(brandId, theme);
            } catch (NumberFormatException e) {
                //不需要处理
            }
        }

        if (conf == null && StringUtils.isNotBlank(confId)) {
            conf = properties.findConfig(confId, theme).orElse(null);
        }

        if (conf == null) {
            log.warn("没有找到配置, 返回默认的通知对象 confId {} theme {}", confId, theme);
            conf = new DingTalkRobotProperties.NotifyThemeConf()
                    .setAccessToken(properties.getAccessToken())
                    .setSignKey(properties.getSignKey());
        }

        return new DingTalkRobotNotifierImpl(conf.getAccessToken(), this.executor)
                .setSignKey(conf.getSignKey());
    }

}
